﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer.Tables;
using System.Data.SqlClient;

public partial class Admin_Users : ChildPage
{
    #region Function
    private void Kaydet()
    {
        Db_Users user = new Db_Users();
        if (txtUsr_Kod.Text != "" && txtUsr_Adi.Text != "")
        {
            user.Create_Date = DateTime.Now;
            user.Create_User = 1;
            user.Hidden = true;
            user.Locked = true;
            user.Update_Date = DateTime.Now;
            user.Update_User = 1;
            user.UserAdi = txtUsr_Adi.Text;
            user.UserDonem = txtUsr_Donem.Text;
            user.UserEmail = txtUsr_Email.Text;
            user.UserFirma = txtUsr_Firma.Text;
            user.UserKasaKod = txtUsr_Kasa_Kod.Text;
            user.UserKod = txtUsr_Kod.Text;
            user.UserPrgId = "test";
            user.UserProgramVersiyon = "test";
            user.UserPwd = txtUsr_Pwd.Text;
            user.UserSystemIn = 1;
            user.UserTel = txtUsr_Tel.Text;
            if (chkusr_yetkili.Checked)
                user.UserYetkili = true;
            else
                user.UserYetkili = false;
            if (drpusr_depo_kod.SelectedIndex != -1)
                user.UserDepoKod = drpusr_depo_kod.SelectedItem.Value;
            user.Save();
            CreateArea();
        }
        else
        {
            if (txtUsr_Kod.Text == "")
                CrmControls.MessageBx.Show("Lütfen Kullanıcı Kodunu Girin!..");
            else if (txtUsr_Adi.Text == "")
                CrmControls.MessageBx.Show("Lütfen Kullanıcı Adını Girin!..");
        }

    }
    private void CreateArea()
    {
        foreach (System.Web.UI.Control c in pnlControls.Controls)
        {
            if (c is CrmControls.CrmTextBox)
            {
                CrmControls.CrmTextBox txt = c as CrmControls.CrmTextBox;
                txt.Text = "";
            }
            if (c is CrmControls.CrmDropDown)
            {
                CrmControls.CrmDropDown drop = c as CrmControls.CrmDropDown;
                if (drop.SelectedIndex != -1)
                    drop.SelectedIndex = 0;
            }
        }
    }
    void DropDoldur()
    {
        Db_FiyatTanim ft = new Db_FiyatTanim();
        DataTable dt = ft.GetAllRecord_Datatable("fyt_kod", "fyt_adi ");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            lst.Value = dt.Rows[i][0].ToString();
            lst.Text = dt.Rows[i][1].ToString();
            drpusr_depo_kod.Items.Add(lst);
        }
    }
    void KayitBul(string id)
    {
        Db_Users user = new Db_Users();
        user.UserKod = id;
        user.KayitBul();
        if (user.UserKod != null || user.UserKod != "")
        {
            txtUsr_Adi.Text = user.UserAdi;
            txtUsr_Donem.Text = user.UserDonem;
            txtUsr_Email.Text = user.UserEmail;
            txtUsr_Firma.Text = user.UserFirma;
            txtUsr_Kasa_Kod.Text = user.UserKasaKod;
            txtUsr_Kod.Text = user.UserKod;
            if (user.UserPwd != null)
            {
                txtUsr_Pwd.TextMode = TextBoxMode.SingleLine;
                txtUsr_Pwd.Text = user.UserPwd;
            }
            txtUsr_Tel.Text = user.UserTel;
            if (user.UserYetkili == true)
                chkusr_yetkili.Checked = true;
            else
                chkusr_yetkili.Checked = false;
            if (user.UserDepoKod != null)
                drpusr_depo_kod.SelectedValue = user.UserDepoKod;
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        imgbtnKaydetKapat.Attributes.Add("onclick", "return Kontrol();");
        imgbtnKaydet.Attributes.Add("onclick", "return Kontrol();");
        imgbtnSil.Attributes.Add("onclick", "return SilKontrol();");
        if (!IsPostBack)
        {
            if (Request.QueryString.Count > 0)
            {
                string id = Request.QueryString["id"];
                KayitBul(id);
            }
        }
        DropDoldur();
    }

    protected void imgbtnKaydet_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Tazele", "Refresh();", true);
    }

    protected void imgbtnKaydetKapat_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "PageClose();", true);
    }

    protected void imgbtnSil_Click(object sender, ImageClickEventArgs e)
    {
        Db_Users user = new Db_Users();
        if (txtUsr_Kod.Text != "")
        {
            user.UserKod = txtUsr_Kod.Text;
            user.KayitSil();
            CreateArea();
        }
        else
            CrmControls.MessageBx.Show("LÜtfen Kullanıcı Kodunu Boş Bırakmayın!..");
        this.ClientScript.RegisterStartupScript(typeof(Page), "Yenile", "Refresh();", true);
    }
    protected void imgNewREcord_Click(object sender, ImageClickEventArgs e)
    {
        CreateArea();
    }
}
